# get GPS of cross-road of qingdao
# tang-xg@qq.com 20200404
# 可用
#依次将找到的路口gps点存下来，高德路口比百度更全，改用高德, 完了又发现百度的geocoding，也再用下百度的更直接。

#https://developer.baidu.com/map/skins/MySkin/resources/iframs/webservice-placapi.html
#http://api.map.baidu.com/place/search?&query='福州路'&region='青岛'&output=xml&key=37492c0ee6f924cb5e934fa08c6b1676


import numpy as np
import pandas as pd
import os
import pickle as pkl

os.chdir("/home/ubuntu/txg/traf_qingdao/semi-final")

#1 import meta-data
pwd = os.getcwd()
train_path = pwd + "/train/"
test_path = pwd + "/test_user/"
# load data of preliminary contest
[flow,relate_cross,crossName,target_cross,pred_cross,similar_cross,roadNet,submitList0,data_tr]=pkl.load(open(pwd+"/../data.pkl",'rb'))
gps_cross = set(target_cross) | set(pred_cross) # merge, gps_cross: cross need gps
crossName.index = crossName['crossroadID']
crossName['lon']=0.0
crossName['lat']=0.0
#gps_road = set() # gps_road: road need gps
#for gc in gps_cross:
#    cross_ = crossName.loc[gc]
#    gps_road.add(cross_['road1'])
#    gps_road.add(cross_['road2'])


""""用get方法请求api：这种方式把参数放在直接放在url中"""
import urllib.request,urllib,json # 把这两个库导入
url ='https://restapi.amap.com/v3/place/text'
for k,cross in enumerate(gps_cross):
    print(k)
    # keyStr = crossName.loc[cross]['crossroadName']
    keyStr = crossName.loc[cross]['road1'] +'与'+ crossName.loc[cross]['road2'] + '交叉口'
    keyStr2 = crossName.loc[cross]['road2'] +'与'+ crossName.loc[cross]['road1'] + '交叉口'
    data = {'language':'zh_cn','logversion':'2.0','city':'370200','children':'',\
            'callback':'json','s':'rsv3','sdkversion':'1.3','platform':'JS',\
            'key':'8325164e247e15eea68b59e89200988b','page':'1','offset':'900',\
            'appname':'https://lbs.amap.com/console/show/picker','keywords':keyStr,\
            'csid':'E89A954E-69AF-4A20-8692-3C3E1B6EBDDA'} # appname can be deleted
    data = urllib.parse.urlencode(data)
    url_ = url +'?'+ data
    # 依次将找到的路口gps点存下来，
    response = urllib.request.urlopen(url_)# 用.urlopen打开上一步返回的结果，得到请求后的响应内容
    apicontent = response.read()#将响应内容用read()读取出来
#    print('用get方法请求api')
    #print(apicontent)#打印读取到的内容 
    apicontent = apicontent.decode()
    ddict = json.loads(apicontent[5:-1])
    for k,poi in enumerate(ddict['pois']):
        if poi['name'] == keyStr or poi['name'] == keyStr2:
            location = poi['location'].split(',')
            crossName.loc[cross,'lon'] = location[0]
            crossName.loc[cross,'lat'] = location[1]
        if k >= 3:
            break
            
    
# save key-variables
pkl.dump([flow,relate_cross,crossName,target_cross,pred_cross,similar_cross,roadNet,submitList0,data_tr],open(pwd+"/data.pkl",'wb'))


    
    
# =============================================================================
# =======http request copy from  https://lbs.amap.com/console/show/picker======
# https://restapi.amap.com/v3/place/text?key=8325164e247e15eea68b59e89200988b&
# appname=https://lbs.amap.com/console/show/picker&page=1&offset=900&city=370200&
# callback=jsonp_648405_&logversion=2.0&csid=E89A954E-69AF-4A20-8692-3C3E1B6EBDDA&
# s=rsv3&children=&language=zh_cn&sdkversion=1.3platform=JS&&keywords='福州路与富裕路交叉口' '富裕路 交叉口' 
# =============================================================================





